<template>
  <div class="app-container">
    <!--表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-date-picker
          v-model="searchObj.begin"
          type="date"
          placeholder="选择开始日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="searchObj.end"
          type="date"
          placeholder="选择截止日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-button
        :disabled="btnDisabled"
        type="primary"
        icon="el-icon-search"
        @click="showChart()"
        >查询</el-button
      >
    </el-form>

    <el-row>
      <el-col :span="12"
        ><div id="register_num" style="height: 400px"
      /></el-col>
      <el-col :span="12"><div id="login_num" style="height: 400px" /></el-col>
    </el-row>
    <el-row>
      <el-col :span="12"
        ><div id="video_view_num" style="height: 400px"
      /></el-col>
      <el-col :span="12"><div id="course_num" style="height: 400px" /></el-col>
    </el-row>

    <div class="chart-container">
      <div id="chart" class="chart" style="height: 500px; width: 100%" />
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
import { showData } from "@/api/statistics";

export default {
  data() {
    return {
      searchObj: {},
      btnDisabled: false,
      chartData: {}, // 图表数据
      dayList: []
    };
  },

  methods: {
    // 展示图表
    showChart() {
      showData(this.searchObj).then((response) => {
        this.chartData = response.data;
        this.dayList = response.data.dateList
        this.showChartByType(
          "register_num",
          "学员登录数统计",
          this.chartData.loginNum
        );
        this.showChartByType(
          "login_num",
          "学员注册数统计",
          this.chartData.registerNum
        );
        this.showChartByType(
          "video_view_num",
          "课程播放数统计",
          this.chartData.videoViewNum
        );
        this.showChartByType(
          "course_num",
          "每日新增课程数统计",
          this.chartData.courseNum
        );
        this.setChart()
      });
    },

    showChartByType(type, title, data) {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById(type));

      // 指定图表的配置项和数据
      var option = {
        title: {
          text: title,
        },
        xAxis: {
          data: this.dayList,
        },
        yAxis: {},
        series: [
          {
            type: "line",
            data: data,
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },

    //生成图表
    setChart() {
      // 基于准备好的dom，初始化echarts实例
      this.chart = echarts.init(document.getElementById("chart"));

      // 指定图表的配置项和数据
      var option = {
        title: {
          text: "数据统计区域图",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985",
            },
          },
        },
        dataZoom: [
          {
            show: true,
            height: 30,
            xAxisIndex: [0],
            bottom: 30,
            start: 10,
            end: 80,
            handleIcon:
              "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
            handleSize: "110%",
            handleStyle: {
              color: "#d3dee5",
            },
            textStyle: {
              color: "#fff",
            },
            borderColor: "#90979c",
          },
          {
            type: "inside",
            show: true,
            height: 15,
            start: 1,
            end: 35,
          },
        ],
        legend: {
          data: ["学员登录数", "学员注册数", "课程播放数", "课程新增数"],
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            data: this.dayList,
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "学员登录数",
            type: "line",
            stack: "总量",
            label: {
              show: true,
              position: "top",
            },
            areaStyle: {},
            emphasis: {
              focus: "series",
            },
            data: this.chartData.loginNum,
          },
          {
            name: "学员注册数",
            type: "line",
            stack: "总量",
            label: {
              show: true,
              position: "top",
            },
            areaStyle: {},
            emphasis: {
              focus: "series",
            },
            data: this.chartData.registerNum,
          },
          {
            name: "课程播放数",
            type: "line",
            stack: "总量",
            label: {
              show: true,
              position: "top",
            },
            areaStyle: {},
            emphasis: {
              focus: "series",
            },
            data: this.chartData.videoViewNum,
          },
          {
            name: "课程新增数",
            type: "line",
            stack: "总量",
            label: {
              show: true,
              position: "top",
            },
            areaStyle: {},
            emphasis: {
              focus: "series",
            },
            data: this.chartData.courseNum,
          },
          
        ],
      };

      this.chart.setOption(option);
    },
  },
};
</script>
